package com.atguigu.ssyx.acl.service.serviceimpl;

import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private AdminRoleService adminRoleService;
    @Override
    public IPage<Role> selectPage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
        String rolename = roleQueryVo.getRoleName();
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(rolename)) {
            wrapper.like(Role::getRoleName, rolename);
        }
        IPage<Role> pageModel = baseMapper.selectPage(pageParam, wrapper);
        return pageModel;
    }

    @Override
    public Map<String, Object> findRoleByUserId(Long adminId) {
        //查询所有的角色
        List<Role> allRoleList = baseMapper.selectList(null);

        //拥有角色id
        List<AdminRole> existUserRoleList = adminRoleService.list(
                new QueryWrapper<AdminRole>().eq("admin_id",adminId).select("role_id"));

        List<Long> existRoleList = existUserRoleList.stream().map(c->c.getRoleId()).collect(Collectors.toList());;

        //对角色进行分类
        List<Role> assignRoles = new ArrayList<Role>();
        for(Role role : allRoleList){
           if(existRoleList.contains(role.getId())){
               assignRoles.add(role);
           }
        }

        Map<String,Object> roleMap = new HashMap<>();
        roleMap.put("assignRoles",assignRoles);
        roleMap.put("allRoleList",allRoleList);
        return roleMap;
    }

    @Override
    public void saveUserRoleRealtionship(Long adminId, Long[] roleIds) {
        adminRoleService.remove(new QueryWrapper<AdminRole>().eq("admin_id",adminId));

        //分配新的角色
        List<AdminRole> userRoleList = new ArrayList<>();
        for(Long roleId : roleIds){
            if(StringUtils.isEmpty(roleId))
                continue;
            AdminRole userRole = new AdminRole();
            userRole.setRoleId(roleId);
            userRole.setAdminId(adminId);
            userRoleList.add(userRole);

        }
        adminRoleService.saveBatch(userRoleList);

    }
}
